Skip to content

array_column should not extract non-public properties from objects#5210

Merged
staabm merged 4 commits intophpstan:2.1.xfrom
janedbal:array-column-visibility
Mar 22, 2026
Merged

array_column should not extract non-public properties from objects#5210
staabm merged 4 commits intophpstan:2.1.xfrom
janedbal:array-column-visibility

Conversation

@janedbal
Copy link
Contributor

@janedbal janedbal force-pushed the array-column-visibility branch from f8df7cc to 4252b95 Compare March 12, 2026 13:03
@janedbal janedbal marked this pull request as ready for review March 12, 2026 14:38
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm requested a review from VincentLanglet March 12, 2026 14:42
@janedbal janedbal force-pushed the array-column-visibility branch 2 times, most recently from 4252b95 to acf44c2 Compare March 16, 2026 15:30
Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to assert expectations for classes which define properties via phpdoc like

/**
 * @property int $id
 * @property string $name
 */
class ObjectWithIsset {}

Copy link
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me

@VincentLanglet VincentLanglet requested a review from staabm March 20, 2026 22:30
janedbal and others added 4 commits March 22, 2026 10:47
PHP's array_column respects calling scope visibility. This change:
- Checks scope->canReadProperty() before including a property type
- When both __isset and __get are defined, treats inaccessible
  properties as maybe-accessible (returns generic array)
- For non-final classes, accounts for child classes that could
  override visibility: protected keeps its type (invariant in PHP),
  private becomes mixed (child can redeclare with any type)
- For final classes, inaccessible properties return empty array
- Handles NeverType in index position by falling back to integer keys

Fixes phpstan/phpstan#13573
For non-final classes, a child class can widen visibility:
- protected: type is preserved (invariant in PHP), treat as maybe
- private: child can redeclare with any type, treat as mixed

For final classes, non-accessible properties return empty array.

Add implicit final test for new expression (asFinal via NewHandler).
@Final is just a PHPDoc convention with no runtime enforcement,
so a child class can still extend and override property visibility.
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
@staabm staabm force-pushed the array-column-visibility branch from 81a548c to b5f33c9 Compare March 22, 2026 09:47
@staabm
Copy link
Contributor

staabm commented Mar 22, 2026

lets give it a go.

thank you

@staabm staabm merged commit 193abd5 into phpstan:2.1.x Mar 22, 2026
651 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants